﻿using System;
using System.IO;
using System.Collections.ObjectModel;
using Helper;
using System.Text.RegularExpressions;
using System.Collections.Generic;

namespace Helper
{
    public class GlobalConfigData
    {
        public string ProxyServer; // 代理
        public int ProxyPort; // 代理
        public string ProxyUser; // 代理
        public string ProxyPass; // 代理
        public int ConnectDelayTime = 5; // 链接延时时间
        public int TryTimes = 3; //如果停车失败，重新尝试的次数
        public int RepeatTime = 0; // 循环停车时间(分钟), 0表示只执行一次
        public int HttpTimeout = 120000; // 设置每次链接的超时(秒)，默认是120秒
        public int HttpTryTimes = 3; // 设置每次HTTP请求失败后尝试的次数，默认是3次
        public bool WriteToLogFile; // 是否记录到log.txt文件
    }

    public static class GlobalConfigHelper
    {
        public static GlobalConfigData ReadGlobalConfigData(string filename)
        {
            GlobalConfigData config = new GlobalConfigData();
            try
            {
                string[] lines = File.ReadAllLines(filename);
                foreach (string line in lines)
                {
                    if (line.StartsWith("proxyserver=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.ProxyServer = GetStringValue(line);
                        continue;
                    }

                    if (line.StartsWith("proxyport=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.ProxyPort = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("proxyuser=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.ProxyUser = GetStringValue(line);
                        continue;
                    }

                    if (line.StartsWith("proxypass=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.ProxyPass = GetStringValue(line);
                        continue;
                    }

                    if (line.StartsWith("DelayConnect=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.ConnectDelayTime = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("RepeatTime=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.RepeatTime = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("TryTimes=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.TryTimes = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("HttpTimeout=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.HttpTimeout = GetIntegerValue(line) * 1000;
                        continue;
                    }

                    if (line.StartsWith("HttpTryTimes=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.HttpTryTimes = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("EnableLog=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.WriteToLogFile = GetBoolValue(line);
                        continue;
                    }
                }
            }
            catch (Exception e)
            {
                TraceLog.Error(e.Message);
                return null;
            }

            return config;
        }

        public static void GetIntegerListValue(Collection<int> list, string line)
        {
            string[] idlist = GetStringValue(line).Split(',');
            foreach (string id in idlist)
            {
                int value = ContentHelper.GetInteger(id);
                if (-1 != value)
                {
                    list.Add(value);
                }
            }
        }

        public static bool GetBoolValue(string line)
        {
            return ("1" == GetStringValue(line));
        }

        public static int GetIntegerValue(string line)
        {
            string value = GetStringValue(line);

            return ContentHelper.GetInteger(value);
        }

        public static string GetStringValue(string line)
        {
            return line.Substring(line.IndexOf('=') + 1);
        }
    }
}
